/*
 * swapTest1.c
 *
 *  Created on: May 28, 2013
 *      Author: koldar
 */

#include <stdio.h>
#include <stdlib.h>
#include "solution.h"
#include "tabooState.h"
#include "los.h"
#include "swap.h"
#include "parser.h"
#include "graphbmp-tools.h"

/* caso di test per controllare che la generazione degli archi crossed funziona correttamente
 *
 */


int main(int argn, const char** args){
	FILE* graphFile;
		Pgraph pgraph;
		Psolution psolution;
		PtabooState ptaboostate;
		int N;
		int K;

		PedgeID _12=initEdgeID(0,1);
		validateEdgeID(_12);
		PedgeID _23=initEdgeID(1,2);
		validateEdgeID(_23);
		PedgeID _34=initEdgeID(2,3);
		validateEdgeID(_34);
		PedgeID _45=initEdgeID(3,4);
		validateEdgeID(_45);
		PedgeID _56=initEdgeID(4,5);
		validateEdgeID(_56);
		PedgeID _67=initEdgeID(5,6);
		validateEdgeID(_67);
		PedgeID _78=initEdgeID(6,7);
		validateEdgeID(_78);
		PedgeID _89=initEdgeID(7,8);
		validateEdgeID(_89);
		PedgeID _19=initEdgeID(8,0);
		validateEdgeID(_19);
		PedgeID _37=initEdgeID(4,6);
		validateEdgeID(_37);
		PedgeID _48=initEdgeID(3,7);
		validateEdgeID(_48);


		if (argn==3){
			graphFile = fopen(args[1],"r");
			K = atoi(args[2]);
		}else{
			printf("numero di parametri passati errati.\n");
			return EXIT_FAILURE;
		}
		pgraph=parseGraph(graphFile,&N);
		dijkstraFiller(pgraph);
		initSmartWeights(pgraph);
		psolution=initSolution(&K,&N);
		ptaboostate=initTabooState(&N,&K,0);

		int k=0;
		addEdgeOp(_12,getEdgeGraph(pgraph,_12),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_23,getEdgeGraph(pgraph,_23),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_34,getEdgeGraph(pgraph,_34),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_45,getEdgeGraph(pgraph,_45),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_56,getEdgeGraph(pgraph,_56),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_67,getEdgeGraph(pgraph,_67),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_78,getEdgeGraph(pgraph,_78),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_89,getEdgeGraph(pgraph,_89),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
		addEdgeOp(_19,getEdgeGraph(pgraph,_19),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);

		drawSolutionFromScratch(
								"pictures/swapTest2_prima.bmp",
								psolution,
								pgraph->depot,
								15,
								50,
								7,
								1,
								4,
								2,
								1,
								COLOR_BLACK,
								COLOR_DARK_GREY,
								COLOR_VIOLET);


		printf("swappo gli archi 3-4 e 7-8 in entrambi i modi...\n");

		EdgeID end1=(EdgeID)*_78;
		EdgeID end2=(EdgeID)*_34;
		int i;
		char buffer[50];

		/*for (i=0;i<=SWAP_GENERATIONMODE_1;i++){

			generateCrossedEdges(_34,_78,&end1,&end2,i);

			removeEdgeOp(&end1,getEdgeGraph(pgraph,&end1),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0);
			removeEdgeOp(&end2,getEdgeGraph(pgraph,&end2),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0);
			addEdgeOp(&end1,getEdgeGraph(pgraph,&end1),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);
			addEdgeOp(&end2,getEdgeGraph(pgraph,&end2),psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[k],0,0,true);

			sprintf(buffer,"pictures/swapTest2_mode_%d.bmp",i);
			drawSolutionFromScratch(
				buffer,
				psolution,
				pgraph->depot,
				15,
				50,
				7,
				1,
				4,
				2,
				1,
				COLOR_BLACK,
				COLOR_DARK_GREY,
				COLOR_VIOLET);
		}*/

		printSolution(stdout,psolution);
		//il 3 finale e' dato per il fatto che addEdgeOp da una tenure fissa di 2
		//se questo test è ancora ritenuto interessante, adattare la chiamata all'evoluzione del codice.
		//solveSwapNeighbourhood(pgraph,psolution->states[k],psolution->gMatrix,k,ptaboostate->tls[0],10);

		drawSolutionFromScratch(
						"pictures/swapTest2_dopo_totale.bmp",
						psolution,
						pgraph->depot,
						15,
						50,
						7,
						1,
						4,
						2,
						1,
						COLOR_BLACK,
						COLOR_DARK_GREY,
						COLOR_VIOLET);

		printSolution(stdout,psolution);
		printf("FINE.\n");
}
